package com.leyou.page.controller;

import com.leyou.page.service.PageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

/**
 * @author LT
 * @version 1.0.0
 * @ClassName PageController.java
 * @Description TODO
 * @createTime 2020年05月12日 00:18:00
 */
@Slf4j
@Controller
public class PageController {

    @Autowired
    private PageService pageService;

    /**
     * 回顾：在springmvc中需要配置一个内部资源视图解析器：InternalResourceViewResolver
     * prefix : 前缀     /WEB-INF/views
     * suffix ：后缀     .jsp
     * 寻找JSP路径： prefix + viewName + suffix
     *
     * @return 视图名称
     */
    @GetMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("msg", "Thymeleaf快速入门");
        model.addAttribute("msg2", "Thymeleaf快速白给");
        return "hello";
    }

    /**
     * 跳转到商品详情页
     *
     * @param spuId spu的ID
     * @return Thymeleaf模板名称
     */
    @GetMapping("/item/{spuId}.html")
    public String item(@PathVariable("spuId") Long spuId, Model model) {
        log.info("进来了===>{}", spuId);
        // ========================================
        Map<String, Object> data = pageService.findGoodsData(spuId);
        model.addAllAttributes(data);
        // ========================================
        return "item";
    }
}

